from MySolveEquation import solve
from time import time
import itertools
eqution = '''
    4x_1 + 5x_2 - x_3 + x_4 + 6x_5 = 3
    3x_1 - 9x_2 - 7x_3 + 5x_4 - 10x_5 = 1
    5x_1 + 2x_2 + x_3 - 4x_4 + 7x_5 = 2
'''
print(eqution)
coef = [4, 5, -1, 1, 6, 3, -9, -7, 5, -10, 5, 2, 1, -4, 7]
target = [3, 1, 2]
def f1():
    for x1 in range(10,29):
        for x2 in range(24,46):
            for x3 in range(31):
                x3 = x3-16
                for x4 in range(14):
                    x4 = x4-31
                    for x5 in range(25):
                        x5 =x5-49
                        e1 = 4*x1+5*x2-x3+x4+6*x5==3
                        e2 = 3*x1-9*x2-7*x3+5*x4-10*x5==1
                        e3 = 5*x1+2*x2+x3-4*x4+7*x5==2
                        if e1 and e2 and e3:
                            print(x1,x2,x3,x4,x5)
def f2():
    for i in itertools.product(range(10,29),range(24,46),range(31),range(14),range(25)):
        x1,x2,x3,x4,x5 = i
        x3 = x3 - 16
        x4 = x4 - 31
        x5 = x5 - 49
        e1 = 4 * x1 + 5 * x2 - x3 + x4 + 6 * x5 == 3
        e2 = 3 * x1 - 9 * x2 - 7 * x3 + 5 * x4 - 10 * x5 == 1
        e3 = 5 * x1 + 2 * x2 + x3 - 4 * x4 + 7 * x5 == 2
        if e1 and e2 and e3:
            print(x1,x2,x3,x4,x5)
def f3(coef, target, flag=True):
    result = solve(coef, target, show_specific=flag)
    for n, i in enumerate(result):
        if flag:
            print('x_%d=%s'%(n,i[0]))
        else:
            print(i[0])
t1 = time()
f1()
print()
t2 = time()
f2()
print()
t3 = time()
f3()
t4 = time()
print(f'5重循环穷举耗时=%.3f秒'%(t2-t1))
print(f'迭代器穷举耗时=%.3f秒'%(t3-t2))
print(f'矩阵求解耗时=%.3f秒'%(t4-t3))
